<?php
/**
 * 获取关系
*/
apf_require_class("Bll_BLL");

class Bll_Jx_Relation extends BLL {
    /**
     * 获取部门列表
     * TODO换成单立的部门表，或从oa直接拿数据
     */
    public function get_department_list(){
        $list = $this->get_user_performance_dao()->find(array(),'',0);
        $dep_list = array();
        foreach($list as $val){
            if(empty($dep_list[$val['branchId']])){
                $dep_list[$val['branchId']] = $val['branch'];
            }
        }
        return $dep_list;
    }
    /**
     * 获取员工的直属上线
     */
    public function get_leader($uid){
        return $this->get_user_kp_dao()->find_by_id($uid);
    }
    /**
     * 取出所有的271
     */
    public function get_271_leader(){
        return $this->get_user_kp_dao()->find(array('is271Leader'=>1),'',0);
    }
    /**
     * 获取xu线主管关系
     * @param unknown_type $uid
     * @param unknown_type $leader
     */
    public function get_virtual_leader($uid,$leader){
        return $this->get_virtual_dao()->find_count(array('leaderId'=>$leader,'userId'=>$uid));
    }
    /**
     * 获取员工的下级员工,并查看是否是271提交者
     * 支持工号和id
     * TODO没有实现xu线主管
     */
    public function get_lower_user($code,$flag=3){
        if(is_numeric($code)){
            $info = $this->get_user_info($code);
            if(empty($info)){
                return array();
            }
            $code = $info['code'];
        }
        $_where['superior_code']=$code;
        if($flag!=3){
            $_where['flag']=$flag;
        }
        $lower = $this->get_user_kp_dao()->find($_where,'',0);
        return $lower;
    }
    /**
     * 获取自己的信息
     * @param unknown_type $uid
     */
    public function get_user_info($uid){
        return $this->get_user_kp_dao()->find_by_id($uid);
    }
    /**
     * 根据工号换成uid
     */
    public function get_code_to_uid(){
        $list = $this->get_user_kp_dao()->find(array(),'',0);
        $res = array();
        foreach($list as $val){
            $res[$val['code']] = $val;
        }
        return $res;
    }
    /**
     * 获取应该参加考评的所有人
     */
    public function get_kp_user($flag=0){
        if($flag!=-1){
            $_where = array('flag'=>$flag);
        }
        $data = $this->get_user_kp_dao()->find($_where,'',0);
        $res = array();
        foreach($data as $val){
            $res[$val['user_id']] = $val;
        }
        return $res;
    }
    /**
     * 获取最新在职员工同步到user_info_kp表
     */
    public function init_user(){
        $new_user = array();
        $old_user = $this->get_kp_user(-1);
        foreach($new_user as $uid=> $val){
            if(!empty($old_user[$uid])){
                unset($old_user[$uid]);
                continue;
            }
            $insert = array(
                    'user_id'=>$uid,
                    'name'=>$val['name'],
                    'code'=>$val['code'],
                    'superior_code'=>$val['superior_code'],
                    'enter_date'=>$val['enter_date'],
                    'is271Leader'=>0,
                    'department_id'=>$val['department_id'],
                    );
            $this->get_user_kp_dao()->insert($insert);
        }
        if(!empty($old_user)){//新用户表中没有的
            $del_uids = array_keys($old_user);
            $_where = array(
                    "user_id IN(".implode(',', $del_uids).") AND 1=? "=>1
                    );
            $this->get_user_kp_dao()->remove($_where);
        }
        return count($new_user);
    }
    /**
     * 设置免老
     */
    public function set_no_kp($code){
        return $this->get_user_kp_dao()->update(array('flag'=>1),array('code'=>$code));
    }
    /**
     * 移出离职员工
     */
    public function get_dimission_user(){
        $new_user = array();
        $old_user = $this->get_kp_user(-1);
        foreach($new_user as $uid=> $val){
            if(!empty($old_user[$uid])){
                unset($old_user[$uid]);
                continue;
            }
        }
        $res = array();
        if(!empty($old_user)){//新用户表中没有的
            $res = $old_user;
        }
        //$res = array_slice($res, 10);
        $ress[4555] = $old_user[4555];
        return $ress;
    }
    public function remove_user_kp($user_id){
        $_where = array();
        if (is_numeric($user_id)){
            $_where['user_id'] = $user_id;
        }else{
            $_where["user_id IN(".implode(',',$user_id).") AND 1=?"] = 1;
        }
        return $this->get_user_kp_dao()->remove($_where);
    }
    /**
     * 得到考评的基本信息
     * @param unknown_type $uids
     * @return multitype:unknown
     */
    public function get_users_by_ids($uids){
        $_where = array(
                "user_id IN(".implode(',',$uids).") AND 1=?"=>1
                );
        $data = $this->get_user_kp_dao()->find($_where);
        $res = array();
        if(!empty($data)){
            foreach($data as $val){
                $res[$val['user_id']] = $val;
            }
        }
        return $res;
    }
    /**
     * 返回员工的直属下级，且这一次是需要考评的
     * @param string $code
     */
    public function get_sub_user_list($code,$flag=0){
        $sub_list = $this->get_user_kp_dao()->find(array('superior_code'=>$code,'flag'=>$flag),'',0);
        return $sub_list;
    }
    /**
     * 生成队列,生成作为主管的自己有几个下属
     * array(
     *     1234=>array(
     *         array('code'=>tt,'name'=>2),
     *         array('code'=>xx,'name'=>2)
     *     )
     * )
     */
    public function get_has_sub_user_list(){
        $data = $this->get_user_kp_dao()->find(array(),'',0);
        $res = array();
        foreach($data as $val){
            if(empty($val['superior_code'])){//mike没有上级
                continue;
            }
            $res[$val['superior_code']][] = $val;
        }
        return $res;
    }
    /**
     * 检测传的工号是否有下级,为显示团队使用
     * @param array $codes
     */
    public function check_has_sub_user($codes){
        $res = array();
        $_where = array(
                "superior_code IN('".implode("','", $codes)."') AND 1=?"=>1
                );
        $data = $this->get_user_kp_dao()->find($_where,'',0);
        if(empty($data)){
            return $res;
        }
        $parent = array();
        //作为工号对应有下属
        foreach($data as $val){
            if(isset($parent[$val['superior_code']])){
                continue;
            }
            $parent[$val['superior_code']] = 1;
        }
        foreach($codes as $val){
            if(!empty($parent[$val])){
                $res[$val] = 1 ;
            }else{
                $res[$val] = 0 ;
            }
        }
        return $res;
    }
    private function get_virtual_dao(){
        return $this->get_dao('Dao_jx_VirtualLeader');

    }
    private function get_user_kp_dao (){
        $dao = $this->get_dao('Dao_jx_UserKp');
        return $dao;
    }
    private function get_user_performance_dao(){
        $dao = $this->get_dao('Dao_jx_UserPerformance');
        return $dao;
    }

}